﻿/// <reference path="../GeneratedArtifacts/viewModel.js" />
/// <reference path="../Scripts/c1ls-4.0.20141.30.js" />

if (typeof c1ls === "undefined") {
    window.location.href = "http://bit.ly/c1ls-nuget";
}

// http://bit.ly/c1ls-grid
//
myapp.GridUnboundGroups.WijmoGrid_render = function (element, contentItem) {

    var table = $("<table/>");
    table.appendTo($(element));

    contentItem.value.oncollectionchange = function () {
        var grid = c1ls.getGridContent(table, contentItem);

        var fips = grid.FindColumn("FIPS");
        fips.visible = false;

        var region = grid.FindColumn("Region");
        region.visible = false;

        region.groupInfo = {
            position: "header",
            outlineMode: "startExpanded",
            headerText: "custom"
        };

        function calcRows() {
            return grid.Rows().map(function (r) {
                r[0] = RegionFromFIPS(r[1]);
                return r;
            });
        }

        var year = grid.FindColumn("Year");
        year.dataFormatString = "d0";

        var value = grid.FindColumn("Value");
        value.dataFormatString = "c0";

        table.wijgrid({
            // initialization
            columns: grid.Columns,
            data: calcRows(),

            // grouping
            groupIndent: 24,
            groupText: function (e, args) {
                args.text = args.groupText;
            },

            // editing
            allowEditing: false,
            afterCellUpdate: grid.OnCellUpdate,
            currentCellChanged: grid.OnCellChanged,

            // sorting
            allowSorting: false,
            sorting: grid.Sorting,

            // filtering
            showFilter: false,
            filtering: grid.Filtering,

            // selection
            selectionMode: "singleRow",
            showRowHeader: grid.ShowRowHeader,
            showSelectionOnRender: false,
            cellClicked: grid.OnCellClicked,
            cellStyleFormatter: grid.OnCellFormat
        });
    };

    c1ls.renderControl(contentItem);
};